package com.sjgtw.cloud.app.riskReporting.service.impl;

import com.sjgtw.cloud.app.riskReporting.domain.UserRealLocation;
import com.sjgtw.cloud.app.riskReporting.mapper.UserRealLocationMapper;
import com.sjgtw.cloud.app.riskReporting.service.IUserRealLocationService;
import com.sjgtw.cloud.common.core.constant.CacheConstants;
import com.sjgtw.cloud.common.core.exception.BaseException;
import com.sjgtw.cloud.common.core.utils.SecurityUtils;
import com.sjgtw.cloud.common.redis.service.RedisService;
import com.sjgtw.cloud.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户实时位置Service业务层处理
 * 
 * @author lvyoufang
 * @date 2024-06-01
 */
@Service
public class UserRealLocationServiceImpl implements IUserRealLocationService 
{
    @Autowired
    private UserRealLocationMapper userRealLocationMapper;

    @Autowired
    private RedisService redisService;

    /**
     * 查询用户实时位置
     * 
     * @param userId 用户实时位置主键
     * @return 用户实时位置
     */
    @Override
    public UserRealLocation selectUserRealLocationByUserId(Long userId)
    {
        return userRealLocationMapper.selectUserRealLocationByUserId(userId);
    }

    /**
     * 查询用户实时位置列表
     * 
     * @param userRealLocation 用户实时位置
     * @return 用户实时位置
     */
    @Override
    public List<UserRealLocation> selectUserRealLocationList(UserRealLocation userRealLocation)
    {
        return userRealLocationMapper.selectUserRealLocationList(userRealLocation);
    }

    /**
     * 新增用户实时位置
     * 
     * @param userRealLocation 用户实时位置
     * @return 结果
     */
    @Override
    public int save(UserRealLocation userRealLocation)
    {
        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);
        if (user == null) {
            throw new BaseException("登录信息失效，请重新登录");
        }
        Long userId = user.getUserid();

        UserRealLocation byUserId = userRealLocationMapper.selectUserRealLocationByUserId(userId);
        if (byUserId == null) {
            userRealLocation.setUserId(userId);
            return userRealLocationMapper.insertUserRealLocation(userRealLocation);
        }
        byUserId.setRealTimeLongitude(userRealLocation.getRealTimeLongitude());
        byUserId.setRealTimeLatitude(userRealLocation.getRealTimeLatitude());
        return userRealLocationMapper.updateUserRealLocation(byUserId);
    }

    /**
     * 修改用户实时位置
     * 
     * @param userRealLocation 用户实时位置
     * @return 结果
     */
    @Override
    public int updateUserRealLocation(UserRealLocation userRealLocation)
    {
        return userRealLocationMapper.updateUserRealLocation(userRealLocation);
    }

    /**
     * 批量删除用户实时位置
     * 
     * @param userIds 需要删除的用户实时位置主键
     * @return 结果
     */
    @Override
    public int deleteUserRealLocationByUserIds(String[] userIds)
    {
        return userRealLocationMapper.deleteUserRealLocationByUserIds(userIds);
    }

    /**
     * 删除用户实时位置信息
     * 
     * @param userId 用户实时位置主键
     * @return 结果
     */
    @Override
    public int deleteUserRealLocationByUserId(Long userId)
    {
        return userRealLocationMapper.deleteUserRealLocationByUserId(userId);
    }
}
